3
3
.
.
1
1
.
.
7
7
C
C
o
o
n
n
d
d
i
i
t
t
i
i
o
o
n
n
a
a
l
l
V
V
i
i
e
e
w
w
'
'
s
s
M
M
o
o
d
d
i
i
f
f
i
i
e
e
r
r
I
I
n
n
f
f
o
o
[
[
R
R
]
]
This tutorial shows how to define View's Modifier based on some condition.
E
E
x
x
a
a
m
m
p
p
l
l
e
e
In this example we change the color of Text View based on the value of redBackground Boolean Variable.
MainActivity.kt
package com.example.testcompose
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Text
import androidx.compose.foundation.background
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.setContent
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
var redBackground = true
Text(
text = "Some text",
modifier = Modifier.background( color = if(redBackground) { Color.Red } else { Color.Green } )
)
}
}
}
redBackground = true redBackground = false